package se.energimyndigheten.lampguiden.bulbreplacement;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockDialogFragment;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.LightSource;

public class SetPriceDialogFragment extends SherlockDialogFragment implements OnSeekBarChangeListener {

    private int mPrice;
    private SeekBar mSeekBar;
    private TextView mMessage;
    private int mYears;

    /** Interface for parent activity to implement to get returning result */
    public interface onPriceChangedListener {
        void onPriceChanged(int price);
        int calculateSavings(int price);
        int calculateSavings(LightSource light, int price);
        LightSource[] getLightSources();
    }

    /** Create a new instance of MyDialogFragment, providing "num" as an argument. */
    static SetPriceDialogFragment newInstance(int price, int years) {
        SetPriceDialogFragment frag = new SetPriceDialogFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("price", price);
        args.putInt("years", years);
        frag.setArguments(args);

        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.Theme_Sherlock_Light);
        mPrice = getArguments().getInt("price");
        mYears = getArguments().getInt("years");
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Activity a = getActivity();
        AlertDialog.Builder ab = new AlertDialog.Builder(a);
        LayoutInflater inf = getActivity().getLayoutInflater();
        View custom = inf.inflate(R.layout.window_title, null);
        /*
        ImageView help = (ImageView) custom.findViewById(R.id.help);
        help.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(a, "Would show help...", Toast.LENGTH_SHORT).show();
            }
        }); */
        View view = inf.inflate(R.layout.setprice_dialog, null); 
        mMessage = (TextView) view.findViewById(android.R.id.message);
        mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
        mSeekBar.setOnSeekBarChangeListener(this);
        mSeekBar.setProgress(mPrice);
        ab.setView(view);
        ab.setCustomTitle(custom);
        ab.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((onPriceChangedListener) getSherlockActivity()).onPriceChanged(mPrice);
            }
        }); 
        ab.setNegativeButton(android.R.string.cancel, null);
        return ab.create();
    }
    
    private int stepper(int progress) {
        int fiddle = 0;
        if (progress < 200) {
            int mod = progress % 5;
            if (mod < 3)
                fiddle = - mod;
            else 
                fiddle = 5-mod;
            return progress + fiddle;
        } else { // 400
            int mod = progress % 10;
            if (mod < 5)
                fiddle =  -mod;
            else 
                fiddle = 10-mod;
            return progress + fiddle;
        }
    }
    
    
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mPrice = stepper(progress);
        int savings = ((onPriceChangedListener) getSherlockActivity()).calculateSavings(mPrice);
        String msg;
        if (savings > 0)
            msg = getResources().getString(R.string.price_dialog_message, mPrice, savings, mYears);
        else
            msg = getResources().getString(R.string.price_dialog_loss, mPrice);
        mMessage.setText(msg);
        seekBar.setContentDescription(msg);
        seekBar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
}